package com.lanren.familybookapp.helper;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanren.common.base.impl.BaseRecyclerViewAdapter;
import com.lanren.common.utils.EditTextUtils;
import com.lanren.common.utils.ScreenUtils;
import com.lanren.common.widget.LimitLinearLayout;
import com.lanren.easydialog.DialogViewHolder;
import com.lanren.easydialog.EasyDialog;
import com.lanren.familybookapp.R;

import java.util.List;

/**
 * @ClassName DialogHelper
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/11/4 13:36
 * @Version 1.0
 */
public class DialogHelper {

    private static DialogHelper instance = null;

    private DialogHelper() {

    }

    public static DialogHelper getInstance() {
        if (instance == null) {
            synchronized (DialogHelper.class) {
                if (instance == null) {
                    instance = new DialogHelper();
                }
            }
        }
        return instance;
    }

    public void showMenuDialog(final Context context, final List<String> list, final boolean isWrapContent, final OnSelectedListener onListener) {
        new EasyDialog(context, R.layout.dialog_menu) {
            @Override
            public void onBindViewHolder(DialogViewHolder holder) {
                RecyclerView mRecyclerView = holder.getView(R.id.rv_dialog_menu_list);
                LimitLinearLayout linearLayout = holder.getView(R.id.list_layout);
                linearLayout.setPadding(20, 20, 20, 20);
                LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) linearLayout.getLayoutParams();
                if (isWrapContent) {
                    linearParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                    linearParams.weight = ViewGroup.LayoutParams.MATCH_PARENT;
                } else {
                    linearLayout.setMaxHeight(ScreenUtils.dp2px(context, 260));
                    linearParams.weight = ViewGroup.LayoutParams.MATCH_PARENT;
                }
                linearLayout.setLayoutParams(linearParams);
                mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
                final MenuAdapter mAdapter = new MenuAdapter(context);
                mAdapter.setOnItemClickListener(new BaseRecyclerViewAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
                        dismiss();
                        onListener.onSelected(position, mAdapter.getItem(position));
                    }
                });
                mRecyclerView.setAdapter(mAdapter);
                mAdapter.setData(list);

                holder.setOnClick(R.id.tv_dialog_menu_cancel, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                });

            }
        }
                .backgroundLight(0.2)
                .setCancelAble(true)
                .setCanceledOnTouchOutside(true)
                .fullWidth()
                .fromBottom()
                .showDialog();
    }

    private final class MenuAdapter extends BaseRecyclerViewAdapter<String, MenuAdapter.ViewHolder> {

        private MenuAdapter(Context context) {
            super(context);
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ViewHolder(parent, R.layout.item_dialog_menu);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            holder.mTextView.setText(getItem(position));

            if (position == 0) {
                // 当前是否只有一个条目
                if (getItemCount() == 1) {
                    holder.itemView.setBackgroundResource(R.drawable.dialog_menu_item);
                    holder.mView.setVisibility(View.GONE);
                } else {
                    holder.itemView.setBackgroundResource(R.drawable.dialog_menu_item_top);
                    holder.mView.setVisibility(View.VISIBLE);
                }
            } else if (position == getItemCount() - 1) {
                holder.itemView.setBackgroundResource(R.drawable.dialog_menu_item_bottom);
                holder.mView.setVisibility(View.GONE);
            } else {
                holder.itemView.setBackgroundResource(R.drawable.dialog_menu_item_middle);
                holder.mView.setVisibility(View.VISIBLE);
            }
        }

        final class ViewHolder extends BaseRecyclerViewAdapter.ViewHolder {

            private TextView mTextView;
            private View mView;

            private ViewHolder(ViewGroup parent, int layoutId) {
                super(parent, layoutId);
                mTextView = (TextView) findViewById(R.id.tv_dialog_menu_name);
                mView = findViewById(R.id.v_dialog_menu_line);
            }
        }
    }

    public interface OnSelectedListener {
        /**
         * 选择条目时回调
         */
        void onSelected(int position, String text);
    }

    public void showInputDialog(final Context context, final String title, final int maxEms, final OnClickListener onListener) {
        new EasyDialog(context, R.layout.dialog_input) {
            @Override
            public void onBindViewHolder(DialogViewHolder holder) {
                holder.setText(R.id.tv_dialog_input_title, title);
                final EditText editText = holder.getView(R.id.tv_dialog_input_message);
                editText.setHint("请输入" + title);
                editText.setMaxEms(maxEms);
                EditTextUtils.setEditTextCFilter(editText);
                holder.setOnClick(R.id.tv_dialog_input_cancel, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                    }
                }).setOnClick(R.id.tv_dialog_input_confirm, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dismiss();
                        onListener.onClicks(editText.getText().toString());
                    }
                });
            }
        }
                .backgroundLight(0.2)
                .setCancelAble(true)
                .setCanceledOnTouchOutside(true)
                .fromBottomToMiddle()
                .showDialog();
    }

    public interface OnClickListener {
        /**
         * 选择条目时回调
         */
        void onClicks(String msg);

    }
}
